package com.ljkj.zjf.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DateTool {
	
	
	public static long getDaySub(String beginDateStr,String endDateStr) throws Exception 
    {
		long day = 0;
        SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");    
        Date beginDate = format.parse(beginDateStr);
        Date endDate = format.parse(endDateStr);
 
        day = (endDate.getTime()-beginDate.getTime())/(24*60*60*1000);    
  
        return day;
    }

	public static String getAddDaysDate(String date,int days){

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;

		Calendar calendar = Calendar.getInstance() ;
		try {
			calendar.setTime(sdf.parse(date)) ;
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.DATE,days) ;
		return sdf.format(calendar.getTime()) ;
	}

	public static Date getAddDaysDate(Date date,int days){
		Calendar calendar = Calendar.getInstance() ;
		calendar.setTime(date) ;
		calendar.add(Calendar.DATE,days) ;
		return calendar.getTime() ;
	}
	

	public static String getAddMonthsDate(String pattern, String date,int months){

		SimpleDateFormat sdf = new SimpleDateFormat(pattern);

		Calendar calendar = Calendar.getInstance() ;
		try {
			calendar.setTime(sdf.parse(date)) ;
		} catch (java.text.ParseException e) {
			e.printStackTrace();
		}
		calendar.add(Calendar.MONTH,months) ;
		return sdf.format(calendar.getTime()) ;
	}
	

	public static String getNextMonth(String pattern, String yearmonth,int months){
		
		String regEx = "[^0-9]";
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(yearmonth);
		yearmonth = m.replaceAll("").trim();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		SimpleDateFormat sdf2 = new SimpleDateFormat(pattern);
		Calendar calendar = Calendar.getInstance();
		try {
			Date date = sdf.parse(yearmonth);
			calendar.setTime(date);
			calendar.add(Calendar.MONTH, months);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		return sdf2.format(calendar.getTime());
	}

	public static String getCurrentDate(String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = new Date();
		return sdf.format(date);
	}

	public static int getMonthSpace(String date1, String date2)
			throws Exception {
		int result = 0;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");

		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();

		c1.setTime(sdf.parse(date1));
		c2.setTime(sdf.parse(date2));

		if (c1.compareTo(c2) == 0) {
			return result;
		} else if (c1.after(c2)) {
			while (!c2.after(c1)) {
				result++;
				c2.add(Calendar.MONTH, 1);
			}
			result--;
		} else {
			while (!c1.after(c2)) {
				result++;
				c1.add(Calendar.MONTH, 1);
			}
			result--;
		}
		return result;
	}
	
	public static void main(String[] args) throws ParseException {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//		Date parse = sdf.parse("2014-12-4 15:15");
		
		try {
			System.out.println(getMonthSpace("201510", "201411"));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
}
